package cn.SiFuLan.dao.impl;

import cn.SiFuLan.dao.IbrandDao;
import cn.SiFuLan.entity.brandEntity;
import cn.SiFuLan.utils.BaseDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class brandDaoImpl extends BaseDao implements IbrandDao {
    @Override
    public List<brandEntity> All() {
        String sql="SELECT Brandid,Brand_Info FROM brand";
        List<brandEntity> list=new ArrayList<>();
        ResultSet rs= executeQuery(sql,null);
        while (true) {
            try {
                if (!rs.next()) break;
                brandEntity b=new brandEntity();
                b.setBrandid(rs.getInt("Brandid"));
                b.setBrand_Info(rs.getString("Brand_Info"));
                list.add(b);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        try {
            CloseAll(rs.getStatement().getConnection(),rs.getStatement(),rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<brandEntity> aall() {
        String sql="SELECT Brandid,Brand_Name,imageAddress,initial,Brand_Info,AddTime,UpdateTime FROM brand";
        List<brandEntity> list=new ArrayList<>();
        ResultSet rs= executeQuery(sql,null);
        while (true) {
            try {
                if (!rs.next()) break;
                brandEntity b=new brandEntity();
                b.setBrandid(rs.getInt("Brandid"));
                b.setBrand_Name(rs.getString("Brand_Name"));
                b.setImageAddress(rs.getString("imageAddress"));
                b.setInitial(rs.getString("initial"));
                b.setBrand_Info(rs.getString("Brand_Info"));
                b.setAddTime(rs.getDate("AddTime"));
                b.setUpdateTime(rs.getDate("UpdateTime"));
                list.add(b);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        try {
            CloseAll(rs.getStatement().getConnection(),rs.getStatement(),rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<brandEntity> getAll(brandEntity brand) {
        String sql="SELECT Brandid,Brand_Name,imageAddress,initial,Brand_Info,AddTime,UpdateTime FROM brand where 1 = 1 ";
        List<brandEntity> list=new ArrayList<>();
        List<Object> sz=new ArrayList<>();
        if(brand!=null && !brand.equals("")){
            if(brand.getBrand_Name()!=null && !brand.equals("")){
                sql+=" and Brand_Name like'%"+brand.getBrand_Name()+"%'";
            }
            if(brand.getInitial()!=null && !brand.equals("")){
                sql+=" and Initial like'%"+brand.getInitial()+"%'";
            }
        }
        ResultSet rs= executeQuery(sql,sz);
        while (true) {
            try {
                if (!rs.next()) break;
                brandEntity b=new brandEntity();
                b.setBrandid(rs.getInt("Brandid"));
                b.setBrand_Name(rs.getString("Brand_Name"));
                b.setImageAddress(rs.getString("imageAddress"));
                b.setInitial(rs.getString("initial"));
                b.setBrand_Info(rs.getString("Brand_Info"));
                b.setAddTime(rs.getDate("AddTime"));
                b.setUpdateTime(rs.getDate("UpdateTime"));
                list.add(b);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        try {
            CloseAll(rs.getStatement().getConnection(),rs.getStatement(),rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public boolean add(brandEntity brand) {
        String sql="INSERT INTO brand(Brand_Name,imageAddress,initial,Brand_Info,AddTime,UpdateTime) VALUES (?,?,?,?,?,?) ";
        List<Object> sz=new ArrayList<>();
        sz.add(brand.getBrand_Name());
        sz.add(brand.getImageAddress());
        sz.add(brand.getInitial());
        sz.add(brand.getBrand_Info());
        sz.add(brand.getAddTime());
        sz.add(brand.getUpdateTime());
        return executeUpdate(sql,sz);
    }

    @Override
    public brandEntity getall(int Brandid) {
        String sql="SELECT Brandid,Brand_Name,imageAddress,initial,Brand_Info,AddTime,UpdateTime FROM brand WHERE Brandid = ? ";
        brandEntity b=new brandEntity();
        List<Object> list=new ArrayList<>();
        list.add(Brandid);
        ResultSet rs= executeQuery(sql,list);
        while (true) {
            try {
                if (!rs.next()) break;
                b.setBrandid(rs.getInt("Brandid"));
                b.setBrand_Name(rs.getString("Brand_Name"));
                b.setImageAddress(rs.getString("imageAddress"));
                b.setInitial(rs.getString("initial"));
                b.setBrand_Info(rs.getString("Brand_Info"));
                b.setAddTime(rs.getDate("AddTime"));
                b.setUpdateTime(rs.getDate("UpdateTime"));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        try {
            CloseAll(rs.getStatement().getConnection(),rs.getStatement(),rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  b;
    }

    @Override
    public boolean update(brandEntity brand) {
        String sql="UPDATE brand SET Brand_Name=?,imageAddress=?,initial=?,Brand_Info=?,UpdateTime=? WHERE Brandid=?";
        List<Object> sz=new ArrayList<>();
        sz.add(brand.getBrand_Name());
        sz.add(brand.getImageAddress());
        sz.add(brand.getInitial());
        sz.add(brand.getBrand_Info());
        sz.add(brand.getUpdateTime());
        sz.add(brand.getBrandid());
        return executeUpdate(sql,sz);
    }


}
